package initdb

import (
	baseAuth "gitee.com/zaiqiang231/go-base-app/base_app/auth"
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
)

type initUser struct {
	DbTable
}

func (i initUser) GetTable() store.DbTable {
	return model.User{}
}

func (i initUser) InitData(ds *store.Datastore) {
	password, err := baseAuth.Encrypt("123456")
	if err != nil {
		baseLog.Errorf("initUser InitData fail, %v", err)
		return
	}
	var user = model.User{Name: "admin", Password: password}
	err = ds.DB.Create(&user).Error
	if err != nil {
		baseLog.Errorf("initUser InitData Create fail, %v", err)
		return
	}
	var authorities []model.SysAuthority
	err = ds.DB.Find(&authorities).Error
	if err != nil {
		baseLog.Errorf("initUser InitData find authorities fail, %v", err)
		return
	}

	err = ds.DB.Model(&user).Association("Authorities").Replace(authorities)
	if err != nil {
		baseLog.Errorf("initAuthority InitData update authorities fail, %v", err)
		return
	}
}
